package com.intellij.licensecommon.util;

import java.math.BigInteger;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class LicenseDecoder {
    private static final BigInteger a = new BigInteger("86f71688cdd2612ca117d1f54bdae029", 16);
    private static final BigInteger b = new BigInteger("10001", 16);
    private static final BigInteger c = new BigInteger("846d7cf2385dddd654629dd2ba94ca87", 16);
    private static final BigInteger d = new BigInteger("10001", 16);
    private static final BigInteger e = new BigInteger("1030230a52c274c376605b2c1", 16);
    private static final BigInteger f = new BigInteger("10001", 16);
    public static final long g = 10800000L;
    public static final Date h = a(2003, 9, 15);
    public static final Date i = a(2005, 4, 1);
    public static final Date j = a(2006, 6, 12);
    private static BigInteger k = new BigInteger("b50c87b9b4e1edeb6d737b732290bc61f98d60b7d09521c9bd20beca7cf83af74f49e26f67f1ff79aa77e08b4f13b4469a8b6056dd06c314a97c7cbae4864a6d", 16);
    private static BigInteger l = new BigInteger("10001", 16);

    public static LicenseInfo decodeLicenseKey(String paramString1, String paramString2)
            throws a {
        if (paramString2.length() < 25) {
            return c(paramString1, paramString2);
        }
        LicenseInfo locale = new LicenseInfo();

        locale.b = paramString1;

        int m = 0;
        for (int n = 0; n < paramString2.length(); n++) {
            int i1 = paramString2.charAt(n);
            if (i1 == 45) {
                m++;
            }
        }
        if (m <= 1) {
            return b(paramString1, paramString2);
        }
        if ((m != 5) && (m != 4)) {
            throw new a();
        }
        if (m == 5) {
            n = paramString2.indexOf('-');
            try {
                locale.customerId = Integer.parseInt(paramString2.substring(0, n));
                paramString2 = paramString2.substring(n + 1);
            } catch (NumberFormatException localNumberFormatException) {
                throw new a();
            }
        } else {
            locale.customerId = -1;
        }
        BigInteger localBigInteger1 = LicenseUtil.decodeGroups(paramString2);


        BigInteger localBigInteger2 = localBigInteger1.modPow(b, a);


        Object localObject = localBigInteger2.toByteArray();
        if (localObject.length != 12) {
            byte[] arrayOfByte;
            if (localObject.length == 13) {
                if (localObject[0] == 0) {
                    arrayOfByte = new byte[12];
                    System.arraycopy(localObject, 1, arrayOfByte, 0, 12);
                    localObject = arrayOfByte;
                } else {
                    throw new a();
                }
            } else if (localObject.length < 12) {
                arrayOfByte = new byte[12];
                System.arraycopy(localObject, 0, arrayOfByte, 12 - localObject.length, localObject.length);
                localObject = arrayOfByte;
            } else {
                throw new a();
            }
        }
        if (paramString1 != null) {
            int i2 = m.a(paramString1, locale.customerId, (byte[]) localObject);
            if (localObject[10] != (byte) (i2 & 0xFF)) {
                throw new a();
            }
            if (localObject[11] != (byte) (i2 >> 8 & 0xFF)) {
                throw new a();
            }
        }
        locale.licenseId = (localObject[0] >> 4);
        locale.productId = (localObject[0] & 0xF);

        locale.minorVersion = (localObject[1] >> 4);

        long l1 = (localObject[2] & 0xFF) + ((localObject[3] & 0xFF) << 8) + ((localObject[4] & 0xFF) << 16) + ((localObject[5] & 0xFF) << 24) << 16;


        locale.generationDate = new Date(l1);
        locale.majorVersion = a(localObject[1] & 0xF, locale.generationDate);

        int i3 = (localObject[6] & 0xFF) + ((localObject[7] & 0xFF) << 8);
        if (i3 != 0) {
            locale.expirationDate = new Date(l1 + i3 * 24L * 60L * 60L * 1000L);
        }
        return locale;
    }

    private static LicenseInfo b(String paramString1, String paramString2)
            throws a {
        SubscriptionLicenseInfo localf = new SubscriptionLicenseInfo();

        byte[] arrayOfByte = k.a(paramString2, localf, l, k);

        k.a(arrayOfByte, localf, paramString1, 14);
        k.b(arrayOfByte, localf, paramString1, -1);
        return localf;
    }

    private static LicenseInfo c(String paramString1, String paramString2)
            throws a {
        BigInteger localBigInteger1 = BigInteger.ZERO;
        for (int m = paramString2.length() - 1; m >= 0; m--) {
            int n = paramString2.charAt(m);
            if ((48 <= n) && (n <= 57)) {
                i1 = n - 48;
            } else if ((65 <= n) && (n <= 90)) {
                i1 = n - 65 + 10;
            } else if ((97 <= n) && (n <= 122)) {
                i1 = n - 97 + 36;
            } else if ((n == 60) || (n == 43)) {
                i1 = 62;
            } else if ((n == 62) || (n == 45)) {
                i1 = 63;
            } else {
                throw new a();
            }
            localBigInteger1 = localBigInteger1.shiftLeft(6);
            localBigInteger1 = localBigInteger1.add(BigInteger.valueOf(i1));
        }
        if (paramString2.length() < 20) {
            try {
                return a(paramString1, localBigInteger1);
            } catch (Exception localException) {
            }
        }
        BigInteger localBigInteger2 = localBigInteger1.modPow(d, c);

        Object localObject1 = localBigInteger2.toByteArray();
        int i1 = 14;
        if (localObject1.length != i1) {
            if ((localObject1.length == i1 + 1) && (localObject1[0] == 0)) {
                localObject2 = new byte[i1];
                System.arraycopy(localObject1, 1, localObject2, 0, i1);
                localObject1 = localObject2;
            } else if (localObject1.length < i1) {
                localObject2 = new byte[i1];
                System.arraycopy(localObject1, 0, localObject2, i1 - localObject1.length, localObject1.length);
                localObject1 = localObject2;
            } else {
                throw new a();
            }
        }
        Object localObject2 = new LicenseInfo();

        ((LicenseInfo) localObject2).b = paramString1;
        if (paramString1 != null) {
            i2 = 1976;
            for (int i3 = 0; i3 < paramString1.length(); i3++) {
                i2 = (short) (i2 + paramString1.charAt(i3));
                i2 = (short) (i2 + 666);
                i2 = (short) (i2 ^ 0x7895);
            }
            int tmp381_380 = 0;
            Object tmp381_378 = localObject1;
            tmp381_378[tmp381_380] = ((byte) (tmp381_378[tmp381_380] - (byte) (i2 & 0xFF)));
            int
                    tmp396_395 = 1;
            Object tmp396_393 = localObject1;
            tmp396_393[tmp396_395] = ((byte) (tmp396_393[tmp396_395] - (byte) (i2 >> 8 & 0xFF)));
            if ((localObject1[0] != 0) || (localObject1[1] != 0)) {
                throw new a();
            }
        }
        ((LicenseInfo) localObject2).generationDate = new Date((localObject1[5] & 0xFF) + ((localObject1[6] & 0xFF) << 8) + ((localObject1[7] & 0xFF) << 16) + ((localObject1[8] & 0xFF) << 24) << 16);


        int i2 = (localObject1[9] & 0xFF) + ((localObject1[10] & 0xFF) << 8);
        ((LicenseInfo) localObject2).expirationDate = (i2 != 0 ? new Date(((LicenseInfo) localObject2).generationDate.getTime() + i2 * 24L * 60L * 60L * 1000L) : null);


        ((LicenseInfo) localObject2).licenseId = localObject1[4];

        ((LicenseInfo) localObject2).productId = 1;
        ((LicenseInfo) localObject2).majorVersion = 2;
        ((LicenseInfo) localObject2).minorVersion = 5;

        return localObject2;
    }

    private static LicenseInfo a(String paramString, BigInteger paramBigInteger)
            throws a {
        LicenseInfo locale = new LicenseInfo();

        locale.b = paramString;

        BigInteger localBigInteger = paramBigInteger.modPow(f, e);


        Object localObject = localBigInteger.toByteArray();
        int m = 12;
        if (localObject.length != m) {
            byte[] arrayOfByte;
            if ((localObject.length == m + 1) && (localObject[0] == 0)) {
                arrayOfByte = new byte[m];
                System.arraycopy(localObject, 1, arrayOfByte, 0, m);
                localObject = arrayOfByte;
            } else if (localObject.length < m) {
                arrayOfByte = new byte[m];
                System.arraycopy(localObject, 0, arrayOfByte, m - localObject.length, localObject.length);
                localObject = arrayOfByte;
            } else {
                throw new a();
            }
        }
        if (paramString != null) {
            int n = 1976;
            for (int i1 = 0; i1 < paramString.length(); i1++) {
                n = (short) (n + paramString.charAt(i1));
                n = (short) (n + 666);
                n = (short) (n ^ 0x7895);
            }
            int tmp189_188 = 0;
            Object tmp189_186 = localObject;
            tmp189_186[tmp189_188] = ((byte) (tmp189_186[tmp189_188] - (byte) (n & 0xFF)));
            int
                    tmp204_203 = 1;
            Object tmp204_201 = localObject;
            tmp204_201[tmp204_203] = ((byte) (tmp204_201[tmp204_203] - (byte) (n >> 8 & 0xFF)));
            if ((localObject[0] != 0) || (localObject[1] != 0)) {
                throw new a();
            }
        }
        locale.generationDate = null;
        if ((localObject[5] & 0xFF) != 255) {
            locale.expirationDate = new Date((localObject[2] & 0xFF) + ((localObject[3] & 0xFF) << 8) + ((localObject[4] & 0xFF) << 16) + ((localObject[5] & 0xFF) << 24) << 16);
        } else {
            locale.expirationDate = null;
        }
        locale.licenseId = localObject[10];

        locale.productId = 1;
        locale.majorVersion = 2;
        locale.minorVersion = 0;

        return locale;
    }

    public static int a(int paramInt, Date paramDate) {
        if ((paramInt == 5) && (a(j, paramDate))) {
            return 6;
        }
        if ((paramInt == 4) && (a(i, paramDate))) {
            return 5;
        }
        if ((paramInt == 3) && (a(h, paramDate))) {
            return 4;
        }
        return paramInt;
    }

    public static boolean a(Date paramDate1, Date paramDate2) {
        long l1 = paramDate1.getTime();
        long l2 = paramDate2.getTime();
        return l1 < l2 + 10800000L;
    }

    public static Date a(int paramInt1, int paramInt2, int paramInt3) {
        Calendar localCalendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("Europe/Prague"));
        localCalendar.clear();
        localCalendar.set(paramInt1, paramInt2, paramInt3);
        return localCalendar.getTime();
    }
}

